package com.sitech.portal.server.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.common.service.IAsyncTreeService;
import com.sitech.ibnms.util.RandomGUID;
import com.sitech.portal.server.dao.IShortcutDao;
import com.sitech.portal.server.domain.Shortcut;
import com.sitech.portal.server.domain.ShortcutType;
import com.sitech.portal.server.form.ShortcutForm;
import com.sitech.portal.server.form.ShortcutTypeForm;
import com.sitech.portal.server.service.IShortcutService;

public class ShortcutServiceImpl implements IShortcutService, IAsyncTreeService{

	IShortcutDao shortcutDao;
	
	@Override
	public void delete(ShortcutForm form) throws Exception {
		shortcutDao.delete(new Shortcut(form));
		
	}

	@Override
	public void deleteType(ShortcutTypeForm typeForm) throws Exception {
		shortcutDao.deleteType(new ShortcutType(typeForm));
		
	}

	@Override
	public List<ShortcutForm> query(String loginId) throws Exception {
		List<ShortcutForm> returnRst = new ArrayList<ShortcutForm>();
		List<Shortcut> rst = shortcutDao.queryShortcut(loginId);
		for(Shortcut bean : rst){
			returnRst.add(new ShortcutForm(bean));
		}
		return returnRst;
	}

	@Override
	public List<ShortcutTypeForm> queryType(String loginId) throws Exception {
		List<ShortcutTypeForm> returnRst = new ArrayList<ShortcutTypeForm>();
		List<ShortcutType> rst = shortcutDao.queryType(loginId);
		for(ShortcutType bean : rst){
			returnRst.add(new ShortcutTypeForm(bean));
		}
		return returnRst;
	}

	@Override
	public void save(ShortcutForm form) throws Exception {
		Shortcut st = new Shortcut(form);
		
		if(st.getSHORTCUT_ID()==null || st.getSHORTCUT_ID().equals("")){
			st.setSHORTCUT_ID(RandomGUID.getRandomGUID());
			shortcutDao.save(st);
		}else{
			shortcutDao.update(st);
		}
		
	}

	@Override
	public void saveType(ShortcutTypeForm typeForm) throws Exception {
		ShortcutType st = new ShortcutType(typeForm);
		
		if(typeForm.getType_id()==null||typeForm.getType_id().equals("")){
			st.setTYPE_ID(RandomGUID.getRandomGUID());
			shortcutDao.saveType(st);
		}else{
			shortcutDao.updateType(st);
		}
	}

	public IShortcutDao getShortcutDao() {
		return shortcutDao;
	}

	public void setShortcutDao(IShortcutDao shortcutDao) {
		this.shortcutDao = shortcutDao;
	}

	@Override
	public TreeNode getRootNode(String loginId, String rootId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TreeNode> getSubNodes(String loginId, TreeNode pnode)
			throws Exception {
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		if(pnode.getId()==null || pnode.getId().equals("0")){
			List<ShortcutTypeForm> result = this.queryType(loginId);
			for(ShortcutTypeForm obj : result){
				TreeNode treeNode = new TreeNode();
				treeNode.setId(obj.getType_id());
				treeNode.setName(obj.getType_name());
				treeNode.setpId(pnode.getId());
				treeNodes.add(treeNode);
			}
			
			List<HashMap> resultShortCut = this.queryShortcutByType(loginId, null);
			for(HashMap obj : resultShortCut){
				TreeNode treeNode = new TreeNode();
				treeNode.setId(obj.get("SHORTCUT_ID").toString());
				treeNode.setName(obj.get("SHORTCUT_NAME").toString());
				treeNode.setpId(pnode.getId());
				treeNode.setIsParent(false);
				treeNodes.add(treeNode);
			}
		}else{
			List<HashMap> result = this.queryShortcutByType(loginId, pnode.getId());
			for(HashMap obj : result){
				TreeNode treeNode = new TreeNode();
				treeNode.setId(obj.get("SHORTCUT_ID").toString());
				treeNode.setName(obj.get("SHORTCUT_NAME").toString());
				treeNode.setpId(pnode.getId());
				treeNode.setIsParent(false);
				treeNodes.add(treeNode);
			}
		}
		
		return treeNodes;
	}

	@Override
	public List<HashMap> queryShortcutByType(String loginId, String shortcutType)
			throws Exception {
		// TODO Auto-generated method stub
		return shortcutDao.queryShortcutByType(loginId, shortcutType);
	}

	@Override
	public void updateType(ShortcutTypeForm typeForm) throws Exception {
		ShortcutType st = new ShortcutType(typeForm);
		shortcutDao.updateType(st);
	}

}
